package com.pengtu.web.measure.wt05;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.User;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.entity.receive.Entrust;
import com.pengtu.entity.trial.Order;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.history.HistoryProjectManager;
import com.pengtu.service.quality.FprojectManager;
import com.pengtu.service.receive.TrialCommonManager;
import com.pengtu.service.trial.TrialManager;
import com.pengtu.utils.web.ControlHelpUtil;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({
	@Result(name=BaseActionSupport.RELOAD,location="${ctx}/measure/wt05/order05!all.action?type=${type}",type="redirect"),
	@Result(name = Constants.LAST, location = "${ctx}/measure/wt05/order05!taketask.action?id=${id}&status=${status}&taskid=${taskid}", type = "redirect"),
	@Result(name = Constants.OWN, location = "${ctx}/measure/order/history.action?key=${id}&pngFileName=order.png", type = "redirect")
})
public class Order05Action extends BaseActionSupport<Order> {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;
	private String id;
	private Entrust entrust;
	private Order order;
	private TrialCommonManager trialCommonManager;
	private TrialManager trialManager;
	private FprojectManager fprojectManager;
	private HistoryProjectManager historyProjectManager;
	private Page<Order> page = new Page<Order>(10);
	private String type;
	private String taskid;
	private String status;
	private User user = SpringSecurityUtils.getCurrentUser();

	@Override
	public Order getModel() {
		// TODO Auto-generated method stub
		return order;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String all() throws Exception {
		QueryFilter filters = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		filters.addFilter("trialtype", "WT05");
		filters.addFilter("type", order.getType());
		page = trialManager.findByOrderPage(page, filters);
		System.out.println(page.getResult().size());
		return SUCCESS;
	}

	public void prepareAll() throws Exception {
		prepareModel();
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String login() throws Exception {
		return LOGIN;
	}

	public void prepareLogin() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			order = trialManager.findByOrder("id", id);
		} else {
			order = new Order();
		}
	}

	public String assign() throws Exception {
		return Constants.ASSIGN;
	}

	public void prepareAssign() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			order = trialManager.findByOrder("id", id);
		} else {
			order = new Order();
		}
	}

	/**
	 * 启动流程 start: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String start() throws Exception {
		String deploymentId = fprojectManager.getByKey("order")
				.getDeploymentId();
		String key = Struts2Utils.getParameter("key");
		String value = Struts2Utils.getParameter("value");
		String key1 = Struts2Utils.getParameter("key1");
		String value1 = Struts2Utils.getParameter("value1");
		Map<String, Object> variables = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(key)) {
			variables.put(key, value);
		}
		if (StringUtils.isNotEmpty(key1)) {
			variables.put(key1, value1);
		}
		variables.put("orderId", id);
		type = order.getType();
		trialCommonManager.start(id, deploymentId, "order05", variables);
		trialManager.saveOrder(order);
		return RELOAD;
	}

	public void prepareStart() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			order = trialManager.findByOrder("id", id);
		} else {
			order = new Order();
		}
	}
	
	/**
	 * 业务转发 judge: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String judge() throws Exception {
		taskid = trialCommonManager.isPersonalTasks(user, id, "orderId");
		if (StringUtils.isEmpty(status)) {
			status = order.getStatus();
		}
		if (StringUtils.isNotEmpty(taskid)) {
			return Constants.LAST;
		}
		return Constants.OWN;
	}

	public void prepareJudge() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			order = trialManager.findByOrder("id", id);
		} else {
			order = new Order();
		}
	}
	
	/**
	 * 接受任务 taketask: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String taketask() throws Exception {
		HistoryProject historyProject = historyProjectManager.get(taskid);
		String activityName = trialCommonManager.getTask(taskid)
				.getActivityName();
		if (historyProject == null) {
			historyProject = new HistoryProject();
			historyProject.setKey(id);
			historyProject.setUserName(user.getUserName());
			historyProject.setStatus("000" + status);
			historyProject.setStartDate(new Date());
			historyProject.setTaskId(taskid);
			historyProject.setActivityName(activityName);
		}
		try {
			trialCommonManager.takeTask(taskid, user);
			historyProjectManager.save(historyProject);
		} catch (org.jbpm.api.JbpmException e) {
			historyProjectManager.save(historyProject);
		}
		ControlHelpUtil.setObjectToSession("taskid", taskid);
		if (StringUtils.equals(activityName, "数据采集")) {
			return "part1";
		}
		return null;
	}

	public void prepareTaketask() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			order = trialManager.findByOrder("id", id);
		} else {
			order = new Order();
		}
	}
	
	/**
	 * 数据采集 first: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String first() throws Exception {
		return "first";
	}

	public void prepareFirst() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			order = trialManager.findByOrder("id", id);
		} else {
			order = new Order();
		}
	}
	
	public String two() throws Exception {
		return "two";
	}

	public void prepareTwo() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			order = trialManager.findByOrder("id", id);
		} else {
			order = new Order();
		}
	}
	
	
	
	
	
	

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			order = trialManager.findByOrder("id", id);
		} else {
			order = new Order();
		}
	}

	/**
	 * id
	 * 
	 * @return the id
	 * @since 1.0.0
	 */

	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setTrialCommonManager(TrialCommonManager trialCommonManager) {
		this.trialCommonManager = trialCommonManager;
	}

	/**
	 * entrust
	 * 
	 * @return the entrust
	 * @since 1.0.0
	 */

	public Entrust getEntrust() {
		return entrust;
	}

	/**
	 * @param entrust
	 *            the entrust to set
	 */
	public void setEntrust(Entrust entrust) {
		this.entrust = entrust;
	}

	/**
	 * type
	 *
	 * @return  the type
	 * @since   1.0.0
	 */
	
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * taskid
	 *
	 * @return  the taskid
	 * @since   1.0.0
	 */
	
	public String getTaskid() {
		return taskid;
	}

	/**
	 * @param taskid the taskid to set
	 */
	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}

	/**
	 * status
	 *
	 * @return  the status
	 * @since   1.0.0
	 */
	
	public String getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * page
	 * 
	 * @return the page
	 * @since 1.0.0
	 */

	public Page<Order> getPage() {
		return page;
	}

	/**
	 * @param page
	 *            the page to set
	 */
	public void setPage(Page<Order> page) {
		this.page = page;
	}

	/**
	 * @param trialManager
	 *            the trialManager to set
	 */
	@Autowired
	public void setTrialManager(TrialManager trialManager) {
		this.trialManager = trialManager;
	}

	/**
	 * @param fprojectManager the fprojectManager to set
	 */
	@Autowired
	public void setFprojectManager(FprojectManager fprojectManager) {
		this.fprojectManager = fprojectManager;
	}

	/**
	 * @param historyProjectManager the historyProjectManager to set
	 */
	@Autowired
	public void setHistoryProjectManager(HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

}
